package com.trt.wiki;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * Created by zlm on 2019-10-30.
 */
public class Utils {

    public static byte[] long2Byte(long number) {//long 转 byte
        byte[] b = new byte[8];
        for(int i=0; i<8; i++) {
            b[i] = (byte) (number>>((8-i-1)*8));
        }
        return b;
    }

    // 从offset开始往后的8个字节转换为long数据
    public static long byte2Long(byte[] b, int offset) {
        long end = 0;
        for(int i=0; i<8; i++) {
            end = end | ((long)(b[i+offset] & 0xff)<<((8-i-1)*8));
            // 其中的long类型转换一定要加上，如果没加上结果就成了int类型
        }
        return end;
    }

    public static byte[] int2Byte(int number) {
        byte[] b = new byte[4];
        b[0] = (byte) (number >> 24);
        b[1] = (byte) (number >> 16);
        b[2] = (byte) (number >> 8);
        b[3] = (byte)number;
        return b;
    }

    public static int byte2Int(byte[] b, int offset) {
        int i3 = (b[0+offset] & 0xFF)<< 24;
        int i2 = (b[1+offset] & 0xFF)<< 16;
        int i1 = (b[2+offset] & 0xFF)<< 8;
        int i0 =  b[3+offset] & 0xFF;
        return i3 | i2 | i1 | i0;
    }


    public static void main(String[] args) {



    }

}
